<?php
  include "./include/om_config.inc";
  $smarty = new smartyWWW();
  $msg = "";
  $mcxItem    = array();
  $mcxItemSat = array();
  $foItem     = array();
  $comexItem  = array();
  if(date("D") == "Mon")
  {
    $date = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-2,date("m")));
    $dateDisplay = date("d-m-Y",mktime(0,0,0,date("m"),date("d")-2,date("m")));
  }
  else
  {
    $date = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-1,date("m")));
    $dateDisplay = date("d-m-Y",mktime(0,0,0,date("m"),date("d")-1,date("m")));
  }

  $selectMcxQuery = "SELECT bhavcopyDate,openPrice,highPrice,lowPrice,closePrice,contractCode,
                       CASE contractCode 
                         when 'GOLD' THEN 1 
                         when 'SILVER' THEN 2 
                         when 'COPPER' THEN 3 
                         when 'CRUDEOIL' THEN 4 
                         when 'ZINC' THEN 5 
                         when 'LEAD' THEN 6 
                         when 'NICKEL' THEN 8 
                         when 'NATURALGAS' THEN 9 
                       ELSE contractCode 
                     END AS sortkey FROM bhavcopy
                    WHERE 1 = 1  AND bhavcopyDate = '".$date."'
                    AND (
                        (TRIM(contractCode) like 'GOLD' AND expiryDate       = '".$_POST['goldItem']."')
                     OR (TRIM(contractCode) like 'SILVER' AND expiryDate     = '".$_POST['silverItem']."')
                     OR (TRIM(contractCode) like 'COPPER' AND expiryDate     = '".$_POST['copperItem']."')
                     OR (TRIM(contractCode) like 'CRUDEOIL' AND expiryDate   = '".$_POST['crudeoilItem']."')
                     OR (TRIM(contractCode) like 'ZINC' AND expiryDate       = '".$_POST['zincItem']."')
                     OR (TRIM(contractCode) like 'LEAD' AND expiryDate       = '".$_POST['leadItem']."')
                     OR (TRIM(contractCode) like 'NICKEL' AND expiryDate     = '".$_POST['nickelItem']."')
                     OR (TRIM(contractCode) like 'NATURALGAS' AND expiryDate = '".$_POST['naturalgasItem']."'))
                     ORDER BY sortkey ASC ";
  $selectMcxQueryResult = mysql_query($selectMcxQuery);
  while($row = mysql_fetch_array($selectMcxQueryResult))
    $mcxItem[] = $row;
  $satureDayDate = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-date("w")-1,date("Y")));
  $selectMcxSatQuery = "SELECT bhavcopyDate,openPrice,highPrice,lowPrice,closePrice,contractCode,expiryDate 
                         ,CASE contractCode 
                         when 'GOLD' THEN 1 
                         when 'SILVER' THEN 2 
                         when 'COPPER' THEN 3 
                         when 'CRUDEOIL' THEN 4 
                         when 'ZINC' THEN 5 
                         when 'LEAD' THEN 6 
                         when 'NICKEL' THEN 8 
                         when 'NATURALGAS' THEN 9 
                         ELSE contractCode 
                        END AS sortkey 
                         FROM bhavcopy
                            WHERE 1 = 1  AND bhavcopyDate = '".$satureDayDate."'
                            AND (
                                (TRIM(contractCode) like 'GOLD'       AND expiryDate = '".$_POST['goldItem']."')
                             OR (TRIM(contractCode) like 'SILVER'     AND expiryDate = '".$_POST['silverItem']."')
                             OR (TRIM(contractCode) like 'COPPER'     AND expiryDate = '".$_POST['copperItem']."')
                             OR (TRIM(contractCode) like 'CRUDEOIL'   AND expiryDate = '".$_POST['crudeoilItem']."')
                             OR (TRIM(contractCode) like 'ZINC'       AND expiryDate = '".$_POST['zincItem']."')
                             OR (TRIM(contractCode) like 'LEAD'       AND expiryDate = '".$_POST['leadItem']."')
                             OR (TRIM(contractCode) like 'NICKEL'     AND expiryDate = '".$_POST['nickelItem']."')
                             OR (TRIM(contractCode) like 'NATURALGAS' AND expiryDate = '".$_POST['naturalgasItem']."'))
                             ORDER BY sortkey ASC ";
  $selectMcxSatQueryResult = mysql_query($selectMcxSatQuery);
  while($row = mysql_fetch_array($selectMcxSatQueryResult))
    $mcxItemSat[] = $row;
  $selectFOQuery = "SELECT bhavcopyDate,openPrice,highPrice,lowPrice,closePrice,contractCode,expiryDate FROM bhavcopyf_o
                            WHERE 1 = 1  AND bhavcopyDate = '".$date."'
                            AND ((TRIM(contractCode) like 'NIFTY' AND expiryDate = '".$_POST['foNItem']."')
                                 OR (TRIM(contractCode) like 'BANKNIFTY' AND expiryDate = '".$_POST['foBNItem']."') )
                    ORDER BY contractCode DESC";
  $selectFOQueryResult = mysql_query($selectFOQuery);
  while($row = mysql_fetch_array($selectFOQueryResult))
    $foItem[] = $row;

  $selectComexQuery = "SELECT bhavcopyDate,openPrice,highPrice,lowPrice,closePrice,contractCode FROM bhavcopycomex
                            WHERE 1 = 1  AND bhavcopyDate = '".$date."'";
  $selectComexQueryResult = mysql_query($selectComexQuery);
  while($row = mysql_fetch_array($selectComexQueryResult))
    $comexItem[] = $row;

//  echo "<pre>";
//  print_r($foItem);
  //9909024045 :hitesh call for expirydate
  $smarty->assign("dateDisplay",$dateDisplay);
  $smarty->assign("mcxItem",$mcxItem);
  $smarty->assign("mcxItemSat",$mcxItemSat);
  $smarty->assign("foItem",$foItem);
  $smarty->assign("comexItem",$comexItem);
  $smarty->display("print.tpl");
?>